﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace iLangl.Transport
{
	public class TransportConfiguration : ConfigurationSection
	{
		private static ConfigurationProperty incomePackageFolderProperty;
		private static ConfigurationProperty outcomePackageFolderProperty;
		private static ConfigurationProperty dispatchedPackageFolder;
		private static ConfigurationProperty corruptedPackageFolder;
		
		private static TransportConfiguration instance;
		private static ConfigurationPropertyCollection properties;

		static TransportConfiguration()
		{
			incomePackageFolderProperty = new ConfigurationProperty(
			  "incomePackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			outcomePackageFolderProperty = new ConfigurationProperty(
			  "outcomePackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);


			dispatchedPackageFolder = new ConfigurationProperty(
			  "dispatchedPackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);


			corruptedPackageFolder = new ConfigurationProperty(
			  "corruptedPackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			properties = new ConfigurationPropertyCollection();
			properties.Add(incomePackageFolderProperty);
			properties.Add(outcomePackageFolderProperty);
			properties.Add(dispatchedPackageFolder);
			properties.Add(corruptedPackageFolder);
		}

		private static object mutex = new object();
		/// <summary>
		/// The instance of the TransportConfiguration.
		/// </summary>
		public static TransportConfiguration Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = ConfigurationManager.GetSection("TransportConfiguration") as TransportConfiguration;
						}
					}
				}
				return instance;
			}
		}

		private String _incomePackageFolder;
		/// <summary>
		/// Path to the directory of incoming Packages.
		/// </summary>
		public String IncomePackageFolder
		{
			get
			{
				if (!String.IsNullOrEmpty(_incomePackageFolder))
				{
					return _incomePackageFolder;
				}

				_incomePackageFolder = base[incomePackageFolderProperty] as String;

				if (!Directory.Exists(_incomePackageFolder))
				{
					Directory.CreateDirectory(_incomePackageFolder);
				}

				return _incomePackageFolder;
			}
		}

		private String _outcomePackageFolder;
		/// <summary>
		/// Path to the directory of outcomming Packages.
		/// </summary>
		public String OutcomePackageFolder
		{
			get
			{
				if (!String.IsNullOrEmpty(_outcomePackageFolder))
				{
					return _outcomePackageFolder;
				}

				_outcomePackageFolder = base[outcomePackageFolderProperty] as String;

				if (!Directory.Exists(_outcomePackageFolder))
				{
					Directory.CreateDirectory(_outcomePackageFolder);
				}

				return _outcomePackageFolder;
			}
		}

		public string _corruptedPackageFolder;
		public string CorruptedPackageFolder
		{
			get
			{
				if (!String.IsNullOrEmpty(_corruptedPackageFolder))
				{
					return _corruptedPackageFolder;
				}

				_corruptedPackageFolder = base[corruptedPackageFolder] as String;

				if (!Directory.Exists(_corruptedPackageFolder))
				{
					Directory.CreateDirectory(_corruptedPackageFolder);
				}
				return _corruptedPackageFolder;
			}
		}


		private String _dispatchedPackageFolder = string.Empty;		
		/// <summary>
		/// Path to the directory of disptached Packages.
		/// </summary>
		public String DispatchedPackageFolder
		{
			get
			{
				
				_dispatchedPackageFolder = base[dispatchedPackageFolder] as String;


				String result = Path.Combine(
					_dispatchedPackageFolder,
						String.Format("{0}\\{1}\\{2}\\",
						DateTime.Now.Year.ToString(),
						DateTime.Now.Month.ToString(),
						DateTime.Now.Day.ToString())
						);

				if (!Directory.Exists(result))
				{
					Directory.CreateDirectory(result);
				}

				return result;
			}
		}

		public void SetSettingsForTesting(
			String incomePackageFolder, 
			String outcomePackageFolder,
			String disptachedPackages)
		{
			_incomePackageFolder = incomePackageFolder;
			_outcomePackageFolder = outcomePackageFolder;
			_dispatchedPackageFolder = disptachedPackages;
		}

		protected override ConfigurationPropertyCollection Properties
		{
			get
			{
				return properties;
			}
		}
	}
}